home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / winlib.lzh / WINLIB / IMAGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-23  |  5.1 KB  |  218 lines

  1. /********************************************************************
  2.  *                                                                    *
  3.  *    Image manipulation module                                        *
  4.  *                                                                    *
  5.  *    Copyright (c) Clever Bits and Bitgate Software 1993 - 1994        *
  6.  *    All Rights Reserved.                                            *
  7.  *                                                                    *
  8.  *    This modules deals with all image transforming and scaling etc.    *
  9.  *                                                                    *
  10.  ********************************************************************
  11.  *                                                                    *
  12.  *    Update log:                                                        *
  13.  *                                                                    *
  14.  *    [10.5.93 - 10.7.93] Karl A. 0ygard                                *
  15.  *        not documented                                                *
  16.  *    [2.11.93] Karl A. 0ygard                                        *
  17.  *                            - changed some code layout                *
  18.  *                                                                    *
  19.  ********************************************************************/
  20.  
  21. #include <vdi.h>
  22.  
  23. #include "winlib.h"
  24.  
  25. #ifndef __IMAGES__
  26. #define __IMAGES__
  27. #endif
  28.  
  29. /*
  30.  *    Scale coordinates
  31.  */
  32. LOCAL void scale_coords(int *w, int *h)
  33. {
  34.     *w = (((*w) * image_w) + 7) >> 4;
  35.     *h = (((*h) * image_h) + 7) >> 4;
  36. }
  37.  
  38. /*
  39.  *    Scale image
  40.  *
  41.  *    *src = pointer to image to scale
  42.  *    w, h = width and heigth of image
  43.  *    mode = scaling mode
  44.  *    *b_w, *b_h = pointers to integers to contain new width and height
  45.  *
  46.  *    Returns: nothing
  47.  */
  48. GLOBAL void scale_img(int *src, int w, int h, int mode, int *b_w, int *b_h)
  49. {
  50.     if (src && big_img != TRUE) {
  51.         int pxy[8], dark = mode & DARK_SCALING, vr_mode = dark ? S_OR_D : S_ONLY;
  52.         MFDB image;
  53.         int n_w = w, n_h = h;
  54.  
  55.         scale_coords(&n_w, &n_h);
  56.         *b_w = n_w;
  57.         *b_h = n_h;
  58.  
  59.         init_mfdb(&image, src, w, h, 0, 1);
  60.  
  61.         if (n_h < h) {
  62.             int y, n_y, l_y, flag = TRUE;
  63.  
  64.             pxy[0] = pxy[4] = l_y = n_y = y = 0;
  65.             pxy[2] = pxy[6] = w - 1;
  66.  
  67.             for (; y < h; y++) {
  68.                 if (n_y >= h || dark || y == (h - 1)) {
  69.                     pxy[1] = pxy[3] = y;
  70.                     pxy[5] = pxy[7] = l_y;
  71.                     vro_cpyfm(VDIhandle, flag ? 3 : vr_mode, pxy, &image, &image);
  72.                     if (n_y >= h) {
  73.                         n_y -= h;
  74.                         l_y++;
  75.                         flag = TRUE;
  76.                     }
  77.                     else
  78.                         flag = FALSE;
  79.                 }
  80.                 n_y += n_h;
  81.             }
  82.  
  83.             for (y = n_h - 1; y < h; y++) {
  84.                 pxy[1] = pxy[3] = pxy[5] = pxy[7] = y;
  85.                 vro_cpyfm(VDIhandle, 0, pxy, &image, &image);
  86.             }
  87.             h = n_h;
  88.         }
  89.  
  90.         if (n_w < w) {
  91.             int x, n_x, l_x, flag = TRUE;
  92.  
  93.             pxy[1] = pxy[5] = l_x = n_x = x = 0;
  94.             pxy[3] = pxy[7] = h - 1;
  95.  
  96.             for (; x < w; x++) {
  97.                 if (n_x >= w || dark || x == (w - 1)) {
  98.                     pxy[0] = pxy[2] = x;
  99.                     pxy[4] = pxy[6] = l_x;
  100.                     vro_cpyfm(VDIhandle, (flag) ? 3 : vr_mode, pxy, &image, &image);
  101.  
  102.                     if (n_x >= w) {
  103.                         n_x -= w;
  104.                         l_x++;
  105.                         flag = TRUE;
  106.                     } else
  107.                         flag = FALSE;
  108.                 }
  109.                 n_x += n_w;
  110.             }
  111.  
  112.             for (x = n_w - 1; x < w; x++) {
  113.                 pxy[0] = pxy[2] = pxy[4] = pxy[6] = x;
  114.                 vro_cpyfm(VDIhandle, 0, pxy, &image, &image);
  115.             }
  116.         }
  117.     }
  118. }
  119.  
  120.  
  121. /*
  122.  *    Scale image/bitmap object
  123.  *
  124.  *    *obj = pointer to object to transform
  125.  *    mode = scaling mode (NO_SCALING/SCALING, TEST_SCALING/DARK_SCALING)
  126.  *
  127.  *    Returns: nothing
  128.  */
  129. GLOBAL void scale_image(OBJECT *obj, int mode)
  130. {
  131.     if (mode & SCALING) {
  132.         int dummy;
  133.  
  134.         if (big_img) {
  135.             obj->ob_x += ((gr_cw - (image_w >> 1)) * (obj->ob_width / gr_cw)) >> 1;
  136.             obj->ob_y += ((gr_ch - image_h) * (obj->ob_height / gr_ch)) >> 1;
  137.         } else
  138.             if (obj->ob_type == G_ICON) {
  139.                 ICONBLK *icn = obj->ob_spec.iconblk;
  140.     
  141.                 if (icn->ib_hicon > 3) {
  142.                     scale_img(icn->ib_pdata, icn->ib_wicon, icn->ib_hicon, mode, &dummy, &dummy);
  143.                     scale_img(icn->ib_pmask, icn->ib_wicon, icn->ib_hicon, mode, &dummy, &icn->ib_hicon);
  144.                     scale_coords(&icn->ib_xicon, &icn->ib_yicon);
  145.                     scale_coords(&icn->ib_xtext, &icn->ib_ytext);
  146.                     scale_coords(&icn->ib_xchar, &icn->ib_ychar);
  147.                 }
  148.             } else {
  149.                 BITBLK *blk = obj->ob_spec.bitblk;
  150.     
  151.                 if (blk->bi_hl > 3) {
  152.                     scale_img(blk->bi_pdata, blk->bi_wb << 3, blk->bi_hl, mode, &dummy, &blk->bi_hl);
  153.                     scale_coords(&blk->bi_x, &blk->bi_y);
  154.                 }
  155.             }
  156.     }
  157. }
  158.  
  159.  
  160. /*
  161.  *    Transforms standard raster into device dependent raster
  162.  *
  163.  *    w = width of raster
  164.  *    h = height of raster
  165.  *    *data = pointer to raster data
  166.  */
  167. GLOBAL void vdi_trans(int w, int h, void *data)
  168. {
  169.     if (data) {
  170.         MFDB src, dst;
  171.  
  172.         init_mfdb(&src, (int *) data, w, h, 1, 1);
  173.         init_mfdb(&dst, (int *) data, w, h, 0, 1);
  174.  
  175.         vr_trnfm(VDIhandle, &src, &dst);
  176.     }
  177. }
  178.  
  179.  
  180. /*
  181.  *    Transform icon or bitmap into device dependent raster
  182.  *
  183.  *    *obj = pointer to object
  184.  */
  185. GLOBAL void trans_image(OBJECT *obj)
  186. {
  187.     if (obj->ob_type == G_ICON) {
  188.         ICONBLK *icn = obj->ob_spec.iconblk;
  189.  
  190.         vdi_trans(icn->ib_wicon, icn->ib_hicon, icn->ib_pmask);
  191.         vdi_trans(icn->ib_wicon, icn->ib_hicon, icn->ib_pdata);
  192.     } else {
  193.         BITBLK *img = obj->ob_spec.bitblk;
  194.  
  195.         vdi_trans(img->bi_wb << 3, img->bi_hl, img->bi_pdata);
  196.     }
  197. }
  198.  
  199.  
  200. /*
  201.  *    Initialize mfdb block
  202.  *
  203.  *    *fm = mfdb to initialize
  204.  *    w = width of mfdb
  205.  *    h = height of mfdb
  206.  *    st = 0/1 - standardformat/dependent
  207.  *    pl = bitplanes
  208.  */
  209. GLOBAL void init_mfdb(MFDB *fm, int *adr, int w, int h, int st, int pl)
  210. {
  211.     fm->fd_addr        = adr;
  212.     fm->fd_w        = (w + 15) & 0xfff0;
  213.     fm->fd_h        = h;
  214.     fm->fd_wdwidth    = fm->fd_w >> 4;
  215.     fm->fd_stand    = st;
  216.     fm->fd_nplanes    = pl;
  217. }
  218.